package info.niwota.pasta;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class EditNoteActivity extends Activity implements TextWatcher, OnClickListener {
	private static final String TAG = "EditNoteActivity";
	
	private final static int RC_VOICEINPUT_TITLE = 1;
	
	private final static int RC_VOICEINPUT_BODY = 2;
	
	private Button btnDiscard;

	private Button btnSave;

	private boolean canBack = true;

	private EditText editBody;

	private EditText editTitle;

	public void afterTextChanged(Editable s) {

	}

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	private String getString(Bundle icicle, String key) {
		return (icicle == null ? null : icicle.getString(key));
	}

	private String getString(EditText edit) {
		return (edit == null ? null : edit.getText().toString());
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.note_edit);

		editTitle = (EditText) findViewById(R.id.note_edit_title);
		editBody = (EditText) findViewById(R.id.note_edit_body);

		// Check icicle
		String title = getString(icicle, Note.EXTRA_TITLE);
		String body = getString(icicle, Note.EXTRA_BODY);
		String attach = getString(icicle, Note.EXTRA_ATTACH);
		//
		if (title == null) {
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				title = getString(extras, Note.EXTRA_TITLE);
				body = getString(extras, Note.EXTRA_BODY);
				attach = getString(extras, Note.EXTRA_ATTACH);
			}
		}
		//
		title = (title == null ? "" : title);
		body = (body == null ? "" : body);
		attach = (attach == null ? "" : attach);

		setTitle(R.string.title_note_edit);
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon);
		//

		editTitle.setText(Utils.normalize(title));
		editBody.setText(body);
		
		if (Debug.DEBUG) {
			Log.d(TAG, "title: " + title);
		}
		//
		boolean canSpeak = false; //TODO v1.1 Utils.canSpeak(this);
		
		final ImageButton tviBtn = (ImageButton) findViewById(R.id.note_edit_title_voiceinput);
		final ImageButton bviBtn = (ImageButton) findViewById(R.id.note_edit_body_voiceinput);
		//
		if (canSpeak) {
			tviBtn.setOnClickListener(this);
			bviBtn.setOnClickListener(this);
			tviBtn.setVisibility(View.VISIBLE);
			bviBtn.setVisibility(View.VISIBLE);
		} else {
			tviBtn.setVisibility(View.GONE);
			bviBtn.setVisibility(View.GONE);
		}
		//
		btnSave = (Button) findViewById(R.id.note_edit_save);
		btnSave.setOnClickListener(this);
		
		btnDiscard = (Button) findViewById(R.id.note_edit_discard);
		btnDiscard.setOnClickListener(this);
		
		btnSave.setEnabled(false);
		btnDiscard.setEnabled(true);
		editTitle.addTextChangedListener(this);
		editBody.addTextChangedListener(this);
		//
		canBack = true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (canBack) {
				setResult(RESULT_CANCELED);
				finish();
			}
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(Note.EXTRA_TITLE, Utils.normalize(getString(editTitle)));
		outState.putString(Note.EXTRA_BODY, getString(editBody));
		outState.putString(Note.EXTRA_ATTACH, "");
	}

	public void onTextChanged(CharSequence s, int start, int before, int count) {
		//title is required, at least one non space character
		boolean save = false;
		int len = editTitle.length();
		if (len > 0) {
			Editable et = editTitle.getText();
			for (int i = 0; i < len; i++) {
				if (!Character.isSpaceChar(et.charAt(i))) {
					save = true;
					break;
				}
			}
		}
		btnSave.setEnabled(save);
		canBack = !save;
	}

	@Override
	protected void onActivityResult(int rc, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (rc) {
			case RC_VOICEINPUT_TITLE: {
				String vi = data.getStringExtra(Intents.EXTRA_INPUT);
				editTitle.setText(vi);
				editTitle.append("");
				break;
			}
			case RC_VOICEINPUT_BODY: {
				String vi = data.getStringExtra(Intents.EXTRA_INPUT);
				editBody.setText(vi);
				editBody.append("");
				break;
			}
			default:
			}
		}
	}

	private void doVoiceInput(int rc, int hint, EditText txt) {
		Intent i = Intents.createViIntent();
		i.putExtra(Intents.EXTRA_TITLE, getString(hint));
		i.putExtra(Intents.EXTRA_INPUT, txt.getText().toString());
		startActivityForResult(i, rc);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.note_edit_title_voiceinput:
			doVoiceInput(RC_VOICEINPUT_TITLE, R.string.hint_title, editTitle);
			return;
		case R.id.note_edit_body_voiceinput:
			doVoiceInput(RC_VOICEINPUT_BODY, R.string.hint_body, editBody);
			return;
		case R.id.note_edit_save:
			doSave();
			break;
		case R.id.note_edit_discard: {
			setResult(RESULT_CANCELED);
			finish();
			break;
		}
		default:
		}
	}

	private void doSave() {
		Intent i = new Intent();
		i.putExtra(Note.EXTRA_TITLE, Utils.normalize(getString(editTitle)));
		i.putExtra(Note.EXTRA_BODY, getString(editBody));
		i.putExtra(Note.EXTRA_ATTACH, "");
		setResult(RESULT_OK, i);
		finish();
	}
}
